package com.zhugang.week13.stack;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program algorithms
 * @description: MyStack
 * @author: chanzhugang
 * @create: 2022/11/03 14:52
 */
public class MyStack {

    /**
     * 225 用队列实现栈
     * https://leetcode.cn/problems/implement-stack-using-queues/
     */
    Queue<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }

    /**
     * 入栈倒腾好数据，这样出栈相关操作就直接处理
     *
     * @param x
     */
    public void push(int x) {
        // 插入新数据，把之前旧的数据取出来重新插入到尾部
        int n = queue.size();
        queue.add(x);
        for (int i = 0; i < n; i++) {
            queue.offer(queue.poll());
        }
    }

    public int pop() {
        return queue.poll();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.size() == 0;
    }

}